<?php

namespace Yousheng\Ku;

use Illuminate\Support\ServiceProvider;
use Symfony\Component\Finder\Finder;
use Yousheng\Ku\Commands\DatabaseCreateCommand;
use Yousheng\Ku\Commands\DelCommand;
use Yousheng\Ku\Commands\KuCommand;

class KuServiceProvider extends ServiceProvider
{
    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        // 注册服务绑定
        $this->app->bind(Ku::class, function ($app) {
            return Ku::getInstance();
        });
    }

    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot() {
        if($this->app->runningInConsole()) {
            // $this->commands([
            //    DatabaseCreateCommand::class,
            //    DelCommand::class
            // ]);
            $this->registerCommandsIn(__DIR__.DIRECTORY_SEPARATOR."Commands");
        }
    }
    private function registerCommandsIn(string $directory){
        $commands=[];
        foreach((new Finder)->in($directory)->files() as $file){
            $commandClass = "\\".__NAMESPACE__."\\Commands\\".str_replace('.php','',$file->getBasename());
            if(is_subclass_of($commandClass,KuCommand::class)){
                $commands[]=$commandClass;
            }
        }
        $this->commands($commands);
    }
}